package com.hao.hospital.service;

import java.util.List;

import org.apache.commons.collections.FastArrayList;

import com.alibaba.fastjson.JSONObject;
import com.hao.hospital.model.HospitalPerson;
import com.hao.hospital.model.Person;
import com.hao.hospital.model.PersonUrl;
import com.hao.hospital.model.PersonWorktime;
import com.hao.hospital.util.GeneralMethod;
import com.hao.hospital.util.TimeUtil;
import com.jfinal.aop.Before;
import com.jfinal.aop.Enhancer;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.jlook.jfinal.ext3.kit.SqlBuilder;
import cn.jlook.jfinal.ext3.kit.SqlBuilder.JOIN;

public class PersonService extends AbstractService {
	private static PersonService me;

	public static PersonService me() {
		if (null == me) {
			me = Enhancer.enhance(PersonService.class);
		}
		return me;
	}
	/**
	 * 获取权限列表
	 * @param person_id
	 */
	public List<Record> getPowerList(int person_id) {
		return SqlBuilder.from(PersonUrl.table).alias("pu").join(JOIN.LEFT, Person.table, "p", "p.power=pu.power")
				.where("p.person_id=?").select(person_id);
	}

	/**
	 * 存工作计划表
	 * 
	 * @param person_id
	 * @param worktime
	 */
	public void worktime(int person_id, long worktime) {
		PersonWorktime personWorktime = new PersonWorktime();
		personWorktime.setPersonId(person_id);
		personWorktime.setWorktime(worktime);
		personWorktime.setRegisterNum(20);
		personWorktime.save();
	}

	/**
	 * 分页查询医生信息
	 * 
	 * @param pageNumber
	 * @return
	 */
	public Page<Record> allPersonInfo(int pageNumber,String...condition) {
		String c=GeneralMethod.isCondition(condition[0]);
		String sql="(person_name like "+c+" or person_title like "+c+" or registrationfee like "+c+")";
		if(GeneralMethod.isWork(condition[0])!=0){
			sql=" state="+GeneralMethod.isWork(condition[0]);
		}
		Page<Record> page = SqlBuilder.from(HospitalPerson.table).order("person_id DESC").where(sql).page(pageNumber, 8);
		List<Record> records = page.getList();
		for (Record r : records) {
			r.set("CNstate", GeneralMethod.isState(r.getInt("state")));
		}
		return page;
	}

	public int GetRegisterNum(int person_id, long worktime) {
		return SqlBuilder.from(PersonWorktime.table).where("person_id=? and worktime=?").field("register_num")
				.find(person_id, worktime).getInt("register_num");
	}

	/**
	 * 通过用户登录获取用户信息 和权限对应的列表数据
	 * 
	 * @param code
	 * @return
	 */
	public Record doctorinfo_getBylogin(String uname, String upass) {
		return SqlBuilder.from(HospitalPerson.table).alias("h").field("*")
				.join(JOIN.LEFT, Person.table, "p", "p.person_id=h.person_id").where("username=? and password=?")
				.find(uname, upass);
	}

	/**
	 * 校验密码
	 * 
	 * @param uname
	 * @param upass
	 * @return
	 */
	public boolean checkLogin(String uname, String upass) {
		if (SqlBuilder.from(Person.table).where("username=? and password=?").count(uname, upass) == 1) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param uname
	 * @param upass
	 * @return
	 */
	public boolean changePass(String uname, String upass) {
		if (SqlBuilder.from(Person.table).where("username=?", uname).update("password=?", upass) == 1) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 通过姓名获取单个医生预约余量
	 * 
	 * @param name
	 * @return
	 */
	public List<Record> worktime(String name) {
		Long time = System.currentTimeMillis();
		System.out.println(time);

		List<Record> record = SqlBuilder.from(PersonWorktime.table).alias("p")
				.join(JOIN.LEFT, HospitalPerson.table, "h", "p.person_id=h.person_id").field("*")
				.where("h.person_name=? and p.worktime>? and p.register_num>0").select(name, time);
		System.out.println("查询结果" + record);
		for (Record r : record) {
			r.set("oldWorktime", r.get("worktime"));
			r.set("worktime", TimeUtil.me().stampToDate(r.get("worktime").toString()));
		}
		return record;
	}

	/**
	 * 分页形式获取 全部医生的预约时间和预约量
	 * 
	 * @return
	 */
	public Page<Record> allworktime(int pageNumber) {
		return SqlBuilder.from(PersonWorktime.table).alias("p")
				.join(JOIN.LEFT, HospitalPerson.table, "h", "p.person_id=h.person_id").page(pageNumber, 10);
	}

	/**
	 * 获取所有医护人员信息
	 * 
	 * @return
	 */
	public List<Record> allinfo() {
		return SqlBuilder.from(HospitalPerson.table).where("code not in('GK001','GK002')").select();
	}

	/**
	 * 通过医务人员编号修改医务人员在职状态为离职
	 * 
	 * @param code
	 */
	public int quit(String code) {
		try {
			return SqlBuilder.from(HospitalPerson.table).where("code=?", code).update("state=4");
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 通过用户编码获取用户信息
	 * 
	 * @param code
	 * @return
	 */
	public Record doctorinfo(int personId) {
		Record record= SqlBuilder.from(HospitalPerson.table).where("person_id=?").find(personId);
		record.set("CNstate",GeneralMethod.isState( record.getInt("state")));
		return record;
	}

	/**
	 * 添加医生用户
	 * 
	 * @param data
	 * @return
	 */
	public int addperson(JSONObject data) {
		String code = data.getString("code");
		String person_name = data.getString("person_name");
		String person_title = data.getString("person_title");
		Integer power = data.getInteger("power");
		String department = data.getString("department");
		String uname = data.getString("uname");
		String upass = data.getString("upass");
		HospitalPerson hp = new HospitalPerson();
		Person person = new Person();
		hp.setCode(code);
		hp.setPersonName(person_name);
		hp.setPersonTitle(person_title);
		hp.setDepartment(department);
		person.setUsername(uname);
		person.setPower(power);
		person.setPassword(upass);
		try {
			hp.save();
			person.save();
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 获取预约数量
	 * 
	 * @param person_id
	 * @return
	 */
	public boolean getRegisterNum(int person_id) {
		int i = SqlBuilder.from(PersonWorktime.table).where("person_id=?", person_id).field("register_num").find()
				.getInt("register_num");
		if (i > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 修改余量
	 * 
	 * @param person_id
	 * @return
	 */
	public boolean updateRegisterNum(int person_id, long worktime) {
		System.out.println("OK");
		try {
			int i = SqlBuilder.from(PersonWorktime.table).where("person_id=? and worktime=?", person_id, worktime)
					.update("register_num=register_num-1");
			if (i == 1) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
}
